package com.irdstudio.bhcredit.core.assembly.plugin.fileop;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 数据表操作类-[表名: 01.文件操作配置(plugin_fileop_conf)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-05-05
 */
public class PluginFileopConfDao {

	private static final Logger logger = LoggerFactory.getLogger(PluginFileopConfDao.class);
	/* 连接对象 */
	Connection conn = null;	

	public PluginFileopConfDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public PluginFileopConf queryWithKeys(String pluginConfId,int confSort)
			throws SQLException {
		PluginFileopConf dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM plugin_fileop_conf WHERE plugin_conf_id=? and conf_sort=?");
			ps.setString(1,pluginConfId);
			ps.setInt(2,confSort);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new PluginFileopConf();
				dc.setPluginConfId(rs.getString("plugin_conf_id"));
				dc.setConfSort(rs.getInt("conf_sort"));
				dc.setFileOp(rs.getString("file_op"));
				dc.setFileTarget(rs.getString("file_target"));
				dc.setOpCycleDay(rs.getInt("op_cycle_day"));
				dc.setLastOpDate(rs.getString("last_op_date"));
				dc.setDistanceDay(rs.getInt("distance_day"));
				dc.setLastModifyDate(rs.getString("last_modify_date"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryPluginFileopConfWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dc;
	}
	
	/**
	 * @description:使用pluginConfId查询
	 * @param pluginConfId
	 * @param confSort
	 * @return
	 * @throws SQLException
	 * @date:2018年2月19日 下午3:30:49
	 * @author:hengyh
	 */
	public List<PluginFileopConf> queryWithPluginConfId(String pluginConfId)
			throws SQLException {
		List<PluginFileopConf> dcList = new ArrayList<PluginFileopConf>();
		PluginFileopConf dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM plugin_fileop_conf WHERE plugin_conf_id=? order by conf_sort");
			ps.setString(1,pluginConfId);
			rs = ps.executeQuery();
			while (rs.next()) {
				dc = new PluginFileopConf();
				dc.setPluginConfId(rs.getString("plugin_conf_id"));
				dc.setConfSort(rs.getInt("conf_sort"));
				dc.setFileOp(rs.getString("file_op"));
				dc.setFileTarget(rs.getString("file_target"));
				dc.setOpCycleDay(rs.getInt("op_cycle_day"));
				dc.setLastOpDate(rs.getString("last_op_date"));
				dc.setDistanceDay(rs.getInt("distance_day"));
				dc.setLastModifyDate(rs.getString("last_modify_date"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryPluginFileopConfWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
//	public List<PluginFileopConf> queryWithCond(String szCondition,String szOrderBy)
//			throws SQLException {
//		List<PluginFileopConf> dcList = new ArrayList<PluginFileopConf>();
//		PreparedStatement ps = null;
//		ResultSet rs = null;
//		try {
//			ps = conn.prepareStatement("SELECT * FROM plugin_fileop_conf "
//					+ szCondition + " " + szOrderBy);
//			rs = ps.executeQuery();
//			while (rs.next()) {
//				PluginFileopConf dc = new PluginFileopConf();
//				dc.setPluginConfId(rs.getString("plugin_conf_id"));
//				dc.setConfSort(rs.getInt("conf_sort"));
//				dc.setFileOp(rs.getString("file_op"));
//				dc.setFileTarget(rs.getString("file_target"));
//				dc.setOpCycleDay(rs.getInt("op_cycle_day"));
//				dc.setLastOpDate(rs.getString("last_op_date"));
//				dc.setDistanceDay(rs.getInt("distance_day"));
//				dc.setLastModifyDate(rs.getString("last_modify_date"));
//				dcList.add(dc);
//			}
//		} catch (SQLException e) {
//			throw new SQLException("queryPluginFileopConfWithCond is Wrong!"
//					+ e.getMessage());
//		} finally {
//			close(rs, null, ps);
//		}
//		return dcList;
//	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
	}

}
